home *** CD-ROM | disk | FTP | other *** search
/ IRIX Base Documentation 2002 November / SGI IRIX Base Documentation 2002 November.iso / usr / share / catman / p_man / cat3 / perl5 / constant.z / constant
Encoding:
Text File  |  2002-10-03  |  6.4 KB  |  199 lines

  1.  
  2.  
  3.  
  4. ccccoooonnnnssssttttaaaannnntttt((((3333))))                                                        ccccoooonnnnssssttttaaaannnntttt((((3333))))
  5.  
  6.  
  7.  
  8. NNNNAAAAMMMMEEEE
  9.      constant - Perl pragma to declare constants
  10.  
  11. SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.          use constant BUFFER_SIZE    => 4096;
  13.          use constant ONE_YEAR       => 365.2425 * 24 * 60 * 60;
  14.          use constant PI             => 4 * atan2 1, 1;
  15.          use constant DEBUGGING      => 0;
  16.          use constant ORACLE         => 'oracle@cs.indiana.edu';
  17.          use constant USERNAME       => scalar getpwuid($<);
  18.          use constant USERINFO       => getpwuid($<);
  19.  
  20.          sub deg2rad { PI * $_[0] / 180 }
  21.  
  22.          print "This line does nothing"              unless DEBUGGING;
  23.  
  24.  
  25. DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  26.      This will declare a symbol to be a constant with the given scalar or list
  27.      value.
  28.  
  29.      When you declare a constant such as PI using the method shown above, each
  30.      machine your script runs upon can have as many digits of accuracy as it
  31.      can use. Also, your program will be easier to read, more likely to be
  32.      maintained (and maintained correctly), and far less likely to send a
  33.      space probe to the wrong planet because nobody noticed the one equation
  34.      in which you wrote 3.14195.
  35.  
  36. NNNNOOOOTTTTEEEESSSS
  37.      The value or values are evaluated in a list context. You may override
  38.      this with scalar as shown above.
  39.  
  40.      These constants do not directly interpolate into double-quotish strings,
  41.      although you may do so indirectly. (See the _p_e_r_l_r_e_f manpage for details
  42.      about how this works.)
  43.  
  44.          print "The value of PI is @{[ PI ]}.\n";
  45.  
  46.      List constants are returned as lists, not as arrays.
  47.  
  48.          $homedir = USERINFO[7];             # WRONG
  49.          $homedir = (USERINFO)[7];           # Right
  50.  
  51.      The use of all caps for constant names is merely a convention, although
  52.      it is recommended in order to make constants stand out and to help avoid
  53.      collisions with other barewords, keywords, and subroutine names. Constant
  54.      names must begin with a letter.
  55.  
  56.      Constant symbols are package scoped (rather than block scoped, as use
  57.      strict is). That is, you can refer to a constant from package Other as
  58.      Other::CONST.
  59.  
  60.  
  61.  
  62.  
  63.                                                                         PPPPaaaaggggeeee 1111
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. ccccoooonnnnssssttttaaaannnntttt((((3333))))                                                        ccccoooonnnnssssttttaaaannnntttt((((3333))))
  71.  
  72.  
  73.  
  74.      As with all use directives, defining a constant happens at compile time.
  75.      Thus, it's probably not correct to put a constant declaration inside of a
  76.      conditional statement (like if ($foo) { use constant ... }).
  77.  
  78.      Omitting the value for a symbol gives it the value of undef in a scalar
  79.      context or the empty list, (), in a list context. This isn't so nice as
  80.      it may sound, though, because in this case you must either quote the
  81.      symbol name, or use a big arrow, (=>), with nothing to point to. It is
  82.      probably best to declare these explicitly.
  83.  
  84.          use constant UNICORNS       => ();
  85.          use constant LOGFILE        => undef;
  86.  
  87.      The result from evaluating a list constant in a scalar context is not
  88.      documented, and is nnnnooootttt guaranteed to be any particular value in the
  89.      future. In particular, you should not rely upon it being the number of
  90.      elements in the list, especially since it is not nnnneeeecccceeeessssssssaaaarrrriiiillllyyyy that value
  91.      in the current implementation.
  92.  
  93.      Magical values, tied values, and references can be made into constants at
  94.      compile time, allowing for way cool stuff like this.  (These error
  95.      numbers aren't totally portable, alas.)
  96.  
  97.          use constant E2BIG => ($! = 7);
  98.          print   E2BIG, "\n";        # something like "Arg list too long"
  99.          print 0+E2BIG, "\n";        # "7"
  100.  
  101.  
  102. TTTTEEEECCCCHHHHNNNNIIIICCCCAAAALLLL NNNNOOOOTTTTEEEE
  103.      In the current implementation, scalar constants are actually inlinable
  104.      subroutines. As of version 5.004 of Perl, the appropriate scalar constant
  105.      is inserted directly in place of some subroutine calls, thereby saving
  106.      the overhead of a subroutine call. See the section on _C_o_n_s_t_a_n_t _F_u_n_c_t_i_o_n_s
  107.      in the _p_e_r_l_s_u_b manpage for details about how and when this happens.
  108.  
  109. BBBBUUUUGGGGSSSS
  110.      In the current version of Perl, list constants are not inlined and some
  111.      symbols may be redefined without generating a warning.
  112.  
  113.      It is not possible to have a subroutine or keyword with the same name as
  114.      a constant. This is probably a Good Thing.
  115.  
  116.      Unlike constants in some languages, these cannot be overridden on the
  117.      command line or via environment variables.
  118.  
  119.      You can get into trouble if you use constants in a context which
  120.      automatically quotes barewords (as is true for any subroutine call).  For
  121.      example, you can't say $hash{CONSTANT} because CONSTANT will be
  122.      interpreted as a string.  Use $hash{CONSTANT()} or $hash{+CONSTANT} to
  123.      prevent the bareword quoting mechanism from kicking in.  Similarly, since
  124.      the => operator quotes a bareword immediately to its left you have to say
  125.      CONSTANT() => 'value' instead of CONSTANT => 'value'.
  126.  
  127.  
  128.  
  129.                                                                         PPPPaaaaggggeeee 2222
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. ccccoooonnnnssssttttaaaannnntttt((((3333))))                                                        ccccoooonnnnssssttttaaaannnntttt((((3333))))
  137.  
  138.  
  139.  
  140. AAAAUUUUTTTTHHHHOOOORRRR
  141.      Tom Phoenix, <_r_o_o_t_b_e_e_r@_t_e_l_e_p_o_r_t._c_o_m>, with help from many other folks.
  142.  
  143. CCCCOOOOPPPPYYYYRRRRIIIIGGGGHHHHTTTT
  144.      Copyright (C) 1997, Tom Phoenix
  145.  
  146.      This module is free software; you can redistribute it or modify it under
  147.      the same terms as Perl itself.
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                                         PPPPaaaaggggeeee 3333
  196.  
  197.  
  198.  
  199.